mui.init();
//上传图片实现开始
//本地相册选择 
function galleryImg() {
    plus.gallery.pick(function(a) { 
    plus.io.resolveLocalFileSystemURL(a, function(entry) { 
        plus.io.resolveLocalFileSystemURL("_doc/", function(root) { 
        root.getFile("head.png", {}, function(file) { 
            //文件已存在 
            file.remove(function() { 
            console.log("file remove success"); 
            entry.copyTo(root, 'head.png', function(e) { 
                var e = e.fullPath + "?version=" + new Date().getTime(); 
                uploadHead(e); /*上传图片*/ 
                //变更大图预览的src 
                //目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现 
                },function(e) { 
                            console.log('copy image fail:' + e.message); 
            }); 
            }, function() { 
            console.log("delete image fail:" + e.message); 
            }); 
        }, function() { 
            //文件不存在 
            entry.copyTo(root, 'head.png', function(e) { 
            var path = e.fullPath + "?version=" + new Date().getTime(); 
            uploadHead(path); /*上传图片*/ 
            },function(e) { 
            console.log('copy image fail:' + e.message); 
            }); 
        }); 
        }, function(e) { 
        console.log("get _www folder fail"); 
        }) 
    }, function(e) { 
        console.log("读取拍照文件错误：" + e.message); 
    }); 
    }, function(a) {}, { 
    filter: "image" 
    }) 
}; 
//将图片压缩转成base64 
function getBase64Image(img) { 
    var canvas = document.createElement("canvas"); 
    var width = img.width; 
    var height = img.height; 
    // calculate the width and height, constraining the proportions 
    if(width > height) { 
    if(width > 100) { 
        height = Math.round(height *= 100 / width); 
        width = 100; 
    } 
    } else { 
    if(height > 100) { 
        width = Math.round(width *= 100 / height); 
        height = 100; 
    } 
    } 
    canvas.width = width; /*设置新的图片的宽度*/ 
    canvas.height = height; /*设置新的图片的长度*/ 
    var ctx = canvas.getContext("2d"); 
    ctx.drawImage(img, 0, 0, width, height); /*绘图*/ 
    var dataURL = canvas.toDataURL("image/png", 0.8); 
    return dataURL; 
}
//一次最多选择九张图片
var imgArray=[];// 保留上次选择图片列表
function galleryImgsMaximum(){
    //每次拍摄或选择图片前清空数组对象
    f1.splice(0, f1.length);
    document.getElementsByClassName("showimg")[0].innerHTML = null;
	// 从相册中选择图片
	mui.toast('从相册中选择多张图片(限定最多选择3张)：');
    plus.gallery.pick(function(e){
    	for(var i in e.files){
	    	mui.alert(e.files[i]);
	    	showImg(e.files[i]);
//	    	mui.alert(getBase64Image(e.files[i]))
    	}
    }, function(e){
    	mui.toast('取消选择图片');
    },{filter:'image',multiple:true,maximum:3,system:false,onmaxed:function(){
		plus.nativeUI.alert('最多只能选择3张图片');
    }});// 最多选择3张图片
}
// 全局数组对象，添加文件,用于压缩上传使用
var f1 = new Array();
//转化为base64
function showImg(url) {
    // 兼容以“file:”开头的情况
    if (0 != url.toString().indexOf("file://")) {
        url = "file://" + url;
    }
    var _div_ = document.getElementsByClassName("showimg")[0];
    var _img_ = new Image();
    _img_.src = url; // 传过来的图片路径在这里用。
        _img_.onclick = function() {
                plus.runtime.openFile(url);
            };
    _img_.onload = function() {
        var tmph = _img_.height;
        var tmpw = _img_.width;
        var isHengTu = tmpw > tmph;
        var max = Math.max(tmpw, tmph);
        var min = Math.min(tmpw, tmph);
        var bili = min / max;
        if (max > 1200) {
            max = 1200;
            min = Math.floor(bili * max);
        }
        tmph = isHengTu ? min : max;
        tmpw = isHengTu ? max : min;
        _img_.style.border = "1px solid rgb(200,199,204)";
        _img_.style.margin = "10px";
        _img_.style.width = "150px";
        _img_.style.height = "150px";
        _img_.onload = null;
        plus.io.resolveLocalFileSystemURL(url, function(entry) {
                entry.file(function(file) {
                    console.log(file.size + '--' + file.name);
                    canvasResize(file, {
                        width: tmpw,
                        height: tmph,
                        crop: false,
                        quality: 50, //压缩质量
                        rotate: 0,
                        callback: function(data, width, height) {
                            f1.push(data);
                            _img_.src = data;
                            _div_.appendChild(_img_);
                            plus.nativeUI.closeWaiting();
                        }
                    });
                });
            },
            function(e) {
                plus.nativeUI.closeWaiting();
                console.log(e.message);
            });
    };
};

mui.plusReady(function(){
//	function uploadHead(imgPath) { 
//  var image = new Image(); 
//  image.src = imgPath; 
//  image.onload = function() { 
//  var imgData = getBase64Image(image); 
//  console.log(imgData);
//  mui('#test')[0].src=imgData;
//      }
//  }

	mui('#Addimg')[0].addEventListener('tap',function(){
		galleryImgsMaximum();
		alert(imgArray)
		alert(getBase64Image(imgArray[0]))
	})
	
//底部显示的BUG
		var oldClientHeight = document.documentElement.clientHeight || document.body.clientHeight;
		window.addEventListener('resize', function() {
			var nowClientHeight = document.documentElement.clientHeight || document.body.clientHeight;
			if(oldClientHeight > nowClientHeight) {
				//键盘弹出的事件处理
				mui('#issue')[0].style.display = "none";
			} else {
				//键盘收起的事件处理
				mui('#issue')[0].style.display = "block";
			}
	    })
})

//上传图片实现结束